package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.constants.CommonConstants;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.AgencyCertificationAuditResDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * \* User: chy
 * \* Date: 2025/9/26  17:26
 * \* Description:
 * \
 */
@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements IWorkerCertificationAuditService {
    @Resource
    private IWorkerCertificationService workerCertificationService;

    /**
     * 运营端认证信息分页查询
     *
     * @param queryReqDTO
     * @return
     */
    @Override
    public PageResult<WorkerCertificationAuditResDTO> queryCertificationAuditPage(WorkerCertificationAuditPageQueryReqDTO queryReqDTO) {
        // 分页查询
        Page<WorkerCertificationAudit> page = new Page<>(queryReqDTO.getPageNo(), queryReqDTO.getPageSize());
        // 查询条件
        LambdaUpdateWrapper<WorkerCertificationAudit> wrapper = new LambdaUpdateWrapper<WorkerCertificationAudit>()
                .eq(StringUtils.hasText(queryReqDTO.getName()), WorkerCertificationAudit::getName, queryReqDTO.getName())
                .eq(StringUtils.hasText(queryReqDTO.getIdCardNo()), WorkerCertificationAudit::getIdCardNo, queryReqDTO.getIdCardNo())
                .eq(queryReqDTO.getAuditStatus() != null, WorkerCertificationAudit::getAuditStatus, queryReqDTO.getAuditStatus())
                .eq(queryReqDTO.getCertificationStatus() != null, WorkerCertificationAudit::getCertificationStatus, queryReqDTO.getCertificationStatus());
        Page<WorkerCertificationAudit> pageResult = this.page(page, wrapper);

        return PageUtils.toPage(pageResult, WorkerCertificationAuditResDTO.class);
    }

    /**
     * 审核服务人员认证信息
     *
     * @param id
     */
    @Override
    @Transactional
    public void audit(Long id, CertificationAuditReqDTO auditReqDTO) {
        // 构建审核信息
        WorkerCertificationAudit audit = new WorkerCertificationAudit();
        audit.setId(id);
        audit.setAuditStatus(CommonConstants.AUDIT_STATUS_PASS);
        audit.setRejectReason(auditReqDTO.getRejectReason());
        audit.setCertificationStatus(auditReqDTO.getCertificationStatus());//认证状态，1：认证中，2：认证成功，3认证失败
        audit.setAuditorId(UserContext.currentUserId());//审核人id
        audit.setAuditorName(UserContext.currentUser().getName());//审核人姓名
        audit.setAuditTime(LocalDateTime.now());//审核时间
        this.updateById(audit);
        audit  =  this.getById( id);

        //2.更新服务人员信息
        WorkerCertification workerCertification = new WorkerCertification();
        workerCertification.setId(audit.getServeProviderId());//服务人员id
        workerCertification.setCertificationStatus(audit.getCertificationStatus());//认证状态
        workerCertification.setCertificationTime(audit.getAuditTime());//认证时间
        workerCertificationService.updateById(workerCertification);

    }

}
